package com.kangyi.admin.controller;

import com.kangyi.admin.service.AdminUserService;
import com.kangyi.dto.AddManageAccountDTO;
import com.kangyi.dto.BulkOperationDTO;
import com.kangyi.dto.ManageAccountPageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/manageAccount")
@Slf4j
@Api(tags = "超级管理员-超级管理员-管理员账号管理相关接口")
public class ManageAccountController {

    @Autowired
    private AdminUserService adminUserService;

    @GetMapping("/managePage")
    @ApiOperation("管理员账号分页查询")
    public Result<PageResult> manageAccountPageQuery(ManageAccountPageQueryDTO manageAccountPageQueryDTO){
        log.info("管理员账号分页查询：{}",manageAccountPageQueryDTO);
        try {
            PageResult pageResult = adminUserService.manageAccountPageQuery(manageAccountPageQueryDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除账号")
    @CacheEvict(cacheNames = "manageAccount_", allEntries = true)
    public Result deleteManageAccount(@RequestParam List<Integer> ids){
        log.info("批量删除账号:{}",ids);
        try {
            adminUserService.deleteManageAccount(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/operation")
    @ApiOperation("批量操作启用/禁用")
    @CacheEvict(cacheNames = "manageAccount_", allEntries = true)
    public Result bulkOperation(@RequestBody BulkOperationDTO bulkOperationDTO){
        log.info("批量操作启用/禁用:{}",bulkOperationDTO);
        try {
            adminUserService.bulkOperation(bulkOperationDTO);
            return Result.success("操作成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增超级管理员")
    @CacheEvict(cacheNames = "manageAccount_", allEntries = true)
    public Result addManageAccount(@RequestBody AddManageAccountDTO addManageAccountDTO){
        log.info("新增超级管理员");
        try {
            adminUserService.addManageAccount(addManageAccountDTO);
            return Result.success("新增成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
